import os

from kafka import *

producer_path = '../../trans_file/producer_file/'


def read_piece(file_path):
    with open(file_path,"rb") as r:
        while r.tell() != os.path.getsize(file_path):
            yield r.read(1024*10)


def send_message_piece(file_name):
    i = 1
    producer = KafkaProducer(bootstrap_servers=['master:9092'])
    with open(producer_path+"111.txt",'wb') as w:
        for bytes in read_piece(producer_path+file_name):
            producer.send('testPython', key=i.to_bytes(4,byteorder='big'),value=bytes)
            print('第{}个文件'.format(i))
            i = 1 + i
    producer.flush()
    producer.close()
    print("发送成功！")
    pass



if __name__ == '__main__':
    file_name = 'ten-million.txt'
    send_message_piece(file_name)

    pass